package detplagiasi;

import weka.core.*;
import weka.core.converters.*;
import weka.classifiers.trees.*;
import weka.filters.*;
import weka.filters.unsupervised.attribute.*;
import weka.clusterers.EM;

import java.io.*;
import weka.clusterers.ClusterEvaluation;
import weka.clusterers.DensityBasedClusterer;


public class TextCluster {


  public void startCluster(File[] file) throws Exception {
        TextDirectoryToArff td = new TextDirectoryToArff();
        String addd = Container.getAddress();
            if(addd != null){
                try {
                Instances dataset = td.createDataset(addd);

                ArffSaver saver = new ArffSaver(); //create arff file

                System.out.println("Statistik:");
                System.out.println(dataset.numInstances());
                System.out.println(dataset.numAttributes());
                System.out.println(dataset.toSummaryString());

                StringToWordVector filter = new StringToWordVector();
                filter.setInputFormat(dataset);
                Instances dataFiltered = Filter.useFilter(dataset, filter);


                saver.setInstances(dataFiltered);
                File he = new File(addd + "\\test.arff");
                saver.setFile(he);
                //saver.setDestination(new File(addd + "\\test.arff"));   // **not** necessary in 3.5.4 and later
                saver.writeBatch();
                
                ClusterEvaluation eval;
                Instances               data;
                String[]                options;
                DensityBasedClusterer   cl;

                data = new Instances(new BufferedReader(new FileReader(he)));

                // normal
                System.out.println("\n--> normal");
                options    = new String[2];
                options[0] = "-t";
                options[1] = he.getAbsolutePath();
                System.out.println(
                    ClusterEvaluation.evaluateClusterer(new EM(), options));

                // manual call
                System.out.println("\n--> manual");
                cl   = new EM();
                cl.buildClusterer(data);
                
                eval = new ClusterEvaluation();
                eval.setClusterer(cl);
                eval.evaluateClusterer(new Instances(data));
                System.out.println("# of clusters: " + eval.getNumClusters());
                System.out.println(data.numInstances());

                // density based
                /*
                System.out.println("\n--> density (CV)");
                cl   = new EM();
                eval = new ClusterEvaluation();
                eval.setClusterer(cl);
                eval.crossValidateModel(
                       cl, data, 3, data.getRandomNumberGenerator(1));
                System.out.println("# of clusters: " + eval.getNumClusters());
                * */

                System.out.println(dataset);
                  } catch (Exception e) {
                System.err.println(e.getMessage());
                  }
            }
  }

}
